package hu.issue.entity;

import java.util.LinkedList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name = "users")
public class User {
	@Id
	private String userName;
	@Column(nullable = false)
	private String password;

	@ManyToMany
	@JoinTable(name = "user_roles", joinColumns = @JoinColumn(name = "username"), inverseJoinColumns = @JoinColumn(name = "name"))
	private final List<Role> roles = new LinkedList<Role>();

	protected User() {

	}

	public User(final String userName, final String password) {
		this.userName = userName;
		this.password = password;
	}

	public String getUserName() {
		return this.userName;
	}

	public String getPassword() {
		return this.password;
	}

	public List<Role> getRoles() {
		return this.roles;
	}

	public void setPassword(final String password) {
		this.password = password;
	}

	public boolean isInRole(final String name) {
		for (final Role role : this.roles) {
			if (role.getName().equals(name)) {
				return true;
			}
		}

		return false;
	}
}
